myApp.controller('IntegrationInitController', function ($scope, $stateParams, $http, ngDialog, $uibModal) {
    $scope.info = {};
    $scope.info.index = 1;
    $scope.info.selectIntegration = $stateParams.selectIntegration;
    //集成计划信息
    $scope.info.integrationInfo = {};
    //项目列表
    $scope.info.plans = [];
    $scope.info.selectPlans = [];
    //根据选择项目列出工程列表
    $scope.info.projects = [];
    //第二步选择主干分支后，保存所有项目工程状态
    $scope.info.projectStatus = [];
    //项目负责人检查完毕
    $scope.info.isChecked = false;

    $http.get(beetle_plan_url + '/integration/getIntegrationPlanList?integration_name=' + $scope.info.selectIntegration + '&token_id=' + getTokenId()).success(function (data) {
        $scope.info.integrationInfo = data.data[0];
        $http.get(beetle_plan_url + '/project_plan/getPlanList?isShowOnline=false&token_id=' + getTokenId() + '&start_date=' + $scope.info.integrationInfo.integration_date).success(function (data) {
            $scope.info.plans = [];
            var list = data.data;
            angular.forEach(list, function (plan) {
                if (plan.plan_status == 'TO_INTEGRATION') {
                    $scope.info.plans.push(plan);
                }
            });
        });
    });

    $scope.nextBtn = function () {
        var plans = [];
        if ($scope.info.index == 1) {
            var count = 0;
            $scope.info.selectPlans = [];
            angular.forEach($scope.info.plans, function (plan) {
                if (plan.isIntegration) {
                    count = 1;
                    plans.push(plan.plan_id);
                    $scope.info.selectPlans.push(plan);
                }
            });
            if (count == 0) {
                openDialog(ngDialog, $scope, "请至少选择一个项目!", false, function () {
                });
                return;
            }
        }

        if ($scope.info.index < 3) {
            $scope.info.index = $scope.info.index + 1;
            if ($scope.info.index == 2) {
                $scope.info.projects = [];
                $http.get(beetle_plan_url + '/project_plan/getProjectListForPlans?token_id=' + getTokenId() + '&plan_ids=' + JSON.stringify(plans)).success(function (data) {
                    $scope.info.projects = data.data;
                    angular.forEach($scope.info.projects, function (info) {
                        info.planNames = info.plan_name;
                        info.selectPlan = info;
                        //多个项目包含相同工程  合并
                        if (info.list.length > 0) {
                            info.planNames = "";
                            info.selectPlan = info.list[0];
                            angular.forEach(info.list, function (pro) {
                                info.planNames += pro.plan_name + '|';
                            });
                            info.planNames = info.planNames.substring(0, info.planNames.length - 1);
                        }
                    })
                });
            } else if ($scope.info.index == 3) {
                $scope.info.isChecked = false;
                $scope.info.projectStatus = [];
                angular.forEach($scope.info.projects, function (pro) {
                    if (pro.list.length == 0) {
                        pro.isMerge = false;
                        $scope.info.projectStatus.push(pro);
                    } else {
                        angular.forEach(pro.list, function (info) {
                            if (info.plan_name != pro.selectPlan.plan_name) {
                                info.isMerge = true;
                            } else {
                                info.isMerge = false;
                            }
                            $scope.info.projectStatus.push(info);
                        });
                    }
                });
                var by = function (name, minor) {
                    return function (o, p) {
                        var a, b;
                        if (o && p && typeof o === 'object' && typeof p === 'object') {
                            a = o[name];
                            b = p[name];
                            if (a === b) {
                                return typeof minor === 'function' ? minor(o, p) : 0;
                            }
                            if (typeof a === typeof b) {
                                return a < b ? -1 : 1;
                            }
                            return typeof a < typeof b ? -1 : 1;
                        }
                    }
                };
                $scope.info.projectStatus.sort(by('plan_name', by('trunk_name')));
            }
        } else {
            $scope.info.index = 3;
        }
    };

    $scope.previousBtn = function () {
        if ($scope.info.index > 1) {
            $scope.info.index = $scope.info.index - 1;
        } else {
            $scope.info.index = 1;
        }
    };

    //规划完毕
    $scope.finish = function () {
        var info = $scope.info;
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'finish.html',
            controller: 'IntegrationFinishCtrl',
            size: 'lg',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
    };
});
myApp.controller('IntegrationFinishCtrl', function ($scope, $uibModal, $uibModalInstance, $http, info) {
    $scope.info = info;
    $scope.info.isDeploy = false;
    $scope.info.isEmail = false;

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

    var flag = false;
    $scope.submit = function () {
        if (flag) {
            return;
        }
        flag = true;
        var plannames = [];
        angular.forEach($scope.info.selectPlans, function (plan) {
            plannames.push(plan.plan_name);
        });

        doBeetlePost($http, $uibModal, beetle_plan_url + '/integration/saveIntegrationInfo', {
            token_id: getTokenId(),
            integration_name: $scope.info.selectIntegration,
            isEmail: false,
            isDeloy: false,
            project_list: JSON.stringify($scope.info.projects),
            plan_list: JSON.stringify(plannames)
        }, function (data) {
            if (data.success) {
                $uibModalInstance.dismiss('cancel');
                window.location.href = "#/menu/integration/";
            } else {
                alert(data.msg);
                flag = false;
            }
        });
    };
});